package com.ao.andou.common;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ao.andou.AppConstant;

public final class UserHelper {

    // 表示用户唯一的用户名
    public final static String USER_ID = "db:uid";
    // 用户的个性签名
    public final static String SIGNATURE = "db:signature";
    // 用户的长居地  用户可能没有长居地
    public final static String LOCATION = "db:location";
    // 用户的名号   
    public final static String TITLE = "title";
    // 用户的自我介绍
    public final static String CONTENT = "content";
    // 资源请求地址
    public final static String URI = "uri";
    // link rel="self" same as 资源请求地址
    public final static String SELF = "self";
    // link rel="alternate"    用户的豆瓣页面 
    public final static String ALTERNATE = "alternate";
    // link rel="icon" 用户的头像图片链接   
    public final static String ICON = "icon";
    // link rel="homepage" 用户的个人主页web 
    public final static String HOMEPAGE = "homepage";
    
    // 获取用户信息 
    // GET http://api.douban.com/people/{userID}
    private final static String USER_INRO_URI = "http://api.douban.com/people/";
    
    private final static String SUFFIX = "?apikey=" + AppConstant.CONSUMER_KEY + "&alt=json";
    
    /**
     * Find Douban user info according to the user id
     * @param userId the user id
     * @return user information
     */
    public static HashMap<String, Object> findUser(String userId) {
        
        String url = USER_INRO_URI + userId + SUFFIX;
        String response = ConnectionUtils.doGet(url).toString();
        HashMap<String, Object> user = new HashMap<String, Object>();
        try {
            JSONObject json = new JSONObject(response);
            
            user.put(USER_ID, JsonHelper.getValue(json, USER_ID));
            user.put(SIGNATURE, JsonHelper.getValue(json, SIGNATURE));
            user.put(LOCATION, JsonHelper.getValue(json, LOCATION));
            user.put(CONTENT, JsonHelper.getValue(json, CONTENT));
            user.put(TITLE, JsonHelper.getValue(json, TITLE));
            JsonHelper.getLinkArray(json, user);
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return user;
    }
}
